import React, { useEffect, useState } from "react";
import '../../appcommon.css';
import styles from './receive.module.css';
import { useDispatch } from "react-redux";
import { DotLoading, Mask, Toast } from "antd-mobile";
import { saveEvent } from "@/store/actions/saveEvent";
import icon_share from '@/assets/icon_share.png'
import icon_copy from '@/assets/icon_copy.png'

import NavigateBar from "@/components/NavigateBar";
import QRCode from "qrcode.react";
import { getCoinInfo, isUnvalidUserInfo } from "@/utils/storage";
import { toCopyText } from "@/utils/utils";
import { LangThai } from '@/utils/langThai';
import { useHistory } from "react-router-dom/cjs/react-router-dom.min";

/**
 * 登录页
 */
export default function Receive() {

    //loading状态
    const [maskVisible, setMaskVisible] = useState(false)

    //显示add home弹框
    const [addHomeMaskVisible, setAddHomeMaskVisible] = useState(false);
    const [addHomeMaskIsHomeMode, setAddHomeMaskIsHomeMode] = useState(true);

    // 当前币
    const [curCoinInfo, setCurCoinInfo] = useState({ address: "" });

    // 文案翻译
    const [curLang, setCurLang] = useState(LangThai);

    // 获取 Redux 分发器
    const dispatch = useDispatch()

    //router
    const history = useHistory()

    //router

    //清空登录信息
    useEffect(() => {
        //关闭loading
        setMaskVisible(false)

        // 判断是否需要重登录
        if (isUnvalidUserInfo()) {
            history.replace('/login')
            return
        }

        // 读取缓存
        const coinInfo = getCoinInfo();
        let gCoinInfo = { ...coinInfo };
        setCurCoinInfo(gCoinInfo);

        //打点
        doHitEvent("RECEIVE_START")

        return () => {
            //打点
            doHitEvent("RECEIVE_END")
        }
    }, [])

    //打点接口
    const doHitEvent = async (type, remark = "") => {
        console.log("doHitEvent: " + type + " , " + remark);

        try {
            //获取Action
            const action = saveEvent(type, remark)

            //触发接口
            await dispatch(action)

        } catch (e) {
            console.log(e)
        }
    }

    // 点击 复制
    const onHandleWalletCopy = () => {
        toCopyText(curCoinInfo.address);
    }

    // 点击 分享
    const onHandleWalletShare = () => {
        // Toast.show("Share success");

        // val body = getString(R.string.app_name)//getString(R.string.invite_share_content)
        // val link = mActivityBinding.textReceiveWalletAddress.text.toString()
        //     .trim()//"https://www.tokenplusapp.com?referre=GHU654"
        // val content = body + "\n\n" + link

        let body = "Token+";
        let link = curCoinInfo.address;
        let content = body + "\n\n" + link;
        console.log(content);

        if (navigator.share) {
            navigator.share({
                title: 'Token+',
                text: content,
                url: ''
            })
        } else {
            // console.log('该浏览器不支持系统分享');
            Toast.show(curLang.inviteNotSupportTips);
        }
    }


    return (
        <div className={styles.receiveRoot}>
            <NavigateBar theme="transparent" showLogout={false} onAddFavorite={() => { setAddHomeMaskIsHomeMode(false); setAddHomeMaskVisible(true) }} onAddHome={() => { setAddHomeMaskIsHomeMode(true); setAddHomeMaskVisible(true) }}>{curLang.receiveWalletUnderIdentity}</NavigateBar>

            {/* 使用添加 home screen bar */}
            {/* <AddFavorite onAddHome={() => { setAddHomeMaskIsHomeMode(true); setAddHomeMaskVisible(true) }} /> */}

            <div className="pageContentColumnFull pageContentPaddingHorizontal16px">
                {/* qr code 模块 */}
                <div className={styles.receiveQrCodeFrame}>
                    <div className={styles.receiveQrCodeTitle}>{curLang.receiveOnlySupports} {curCoinInfo.symbol} {curLang.receiveAssets} ({curCoinInfo.name})</div>

                    <div className={styles.receiveQrCode}>
                        <QRCode value={curCoinInfo.address} fgColor="black" includeMargin={true} renderAs="svg" level="H" size={250} bgColor="white"></QRCode>
                    </div>

                    <div className={styles.receiveWalletAddressTitle}>{curLang.receiveWalletAddress}</div>
                    <div className={styles.receiveWalletAddress}>{curCoinInfo.address}</div>

                    <div className={styles.receiveBottomActionFrame}>
                        <div className={styles.receiveBottomAction} onClick={onHandleWalletShare}>
                            <img className={styles.receiveBottomActionIcon} alt="" src={icon_share} />
                            <div className={styles.receiveWalletActionTitle}>{curLang.receiveShare}</div>
                        </div>

                        <div className={styles.receiveBottomAction} onClick={onHandleWalletCopy}>
                            <img className={styles.receiveBottomActionIcon} alt="" src={icon_copy} />
                            <div className={styles.receiveWalletActionTitle}>{curLang.receiveCopy}</div>
                        </div>
                    </div>
                </div>
            </div>

            {/* 底部 margin */}
            <div className="pageBottomEdge"></div>

            {/* load mask */}
            <Mask visible={maskVisible} opacity={0.1}
                onMaskClick={() => { }}>
                <div className="pageLoadingOverlayContent">
                    <span style={{ fontSize: 24, color: '#3498DB' }} >
                        <DotLoading color="currentColor" />
                    </span>
                </div>
            </Mask>
        </div >
    )
}